package com.wei.miaosha.service;

import com.wei.miaosha.dao.MiaoshaUserDao;
import com.wei.miaosha.domain.MiaoshaUser;
import com.wei.miaosha.exception.GlobalException;
import com.wei.miaosha.redis.MiaoShaUserKey;
import com.wei.miaosha.redis.RedisService;
import com.wei.miaosha.result.CodeMsg;
import com.wei.miaosha.util.Md5Util;
import com.wei.miaosha.util.UUIDUtil;
import com.wei.miaosha.vo.LoginVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
/**
 * Created by Weizichen on 2018/8/19.
 * desc:用户秒杀业务层
 */
@Service
public class MiaoshaUserService {
    public static final String COOKIE_NAME_TOKEN = "token";
    @Autowired
    MiaoshaUserDao miaoshaUserDao;

    @Autowired
    RedisService redisService;

    public MiaoshaUser getById(long id) {
        //取缓存
        MiaoshaUser user = redisService.get(MiaoShaUserKey.getById, "" + id, MiaoshaUser.class);
        if (user != null) {
            return user;
        }
        user = miaoshaUserDao.getById(id);
        if (user != null) {
            //存入缓存中
            redisService.set(MiaoShaUserKey.getById, "" + id, user);
        }
        return user;
    }

    public boolean updatePassWord(String token, long id, String formPass) {
        MiaoshaUser user = getById(id);
        if (user == null) {
            new GlobalException(CodeMsg.MOBILE_NOT_EXIST);
        }
        //先更新数据库
        MiaoshaUser updateUser = new MiaoshaUser();
        updateUser.setId(id);
        updateUser.setPassword(Md5Util.formPassToDBPass(formPass, user.getSalt()));
        miaoshaUserDao.update(updateUser);
        //处理缓存
        redisService.delete(MiaoShaUserKey.getById, "" + id);
        //更新token
        user.setPassword(updateUser.getPassword());
        redisService.set(MiaoShaUserKey.token, token, user);
        return true;
    }

    public String login(LoginVo loginVo, HttpServletResponse response) {
        if (loginVo == null) {
            throw new GlobalException(CodeMsg.SERVER_ERROR);
        }
        String mobile = loginVo.getMobile();
        String formPassword = loginVo.getPassword();
        //手机号是否存在
        MiaoshaUser user = getById(Long.parseLong(mobile));
        if (user == null) {
            throw new GlobalException(CodeMsg.MOBILE_NOT_EXIST);
        }
        //验证密码
        String dbPass = user.getPassword();
        String dbSalt = user.getSalt();
        String calcPass = Md5Util.formPassToDBPass(formPassword, dbSalt);
        if (!calcPass.equals(dbPass)) {
            throw new GlobalException(CodeMsg.PASSWORD_ERROR);
        }
        String token = UUIDUtil.uuid();
        addCookie(response, user, token);
        // return true;
        return token;
    }

    public MiaoshaUser getByToken(String token, HttpServletResponse response) {
        if (StringUtils.isEmpty(token)) {
            return null;
        }
        MiaoshaUser user = redisService.get(MiaoShaUserKey.token, token, MiaoshaUser.class);
        if (user != null) {
            addCookie(response, user, token);
        }
        return user;
    }

    private void addCookie(HttpServletResponse response, MiaoshaUser user, String token) {
        redisService.set(MiaoShaUserKey.token, token, user);
        Cookie cookie = new Cookie(COOKIE_NAME_TOKEN, token);
        cookie.setPath("/");
        cookie.setMaxAge(MiaoShaUserKey.token.expireSeconds());
        response.addCookie(cookie);
    }
}
